<template>
  <div class="goods">
    <div class="wrap">
      <Crumb> </Crumb>
      <img src="../assets/img/banner.4c6b6225.png" width="100%" alt="" />
      <ul class="options">
        <li>
          <strong>排序：</strong>
          <span
            @click="options1Fn(index, item.min, item.max)"
            v-for="(item, index) in options1"
            :key="index"
            :class="{ active: index == options1ActiveIndex ? true : false }"
            >{{ item.text }}</span
          >
        </li>
        <li>
          <strong>分类：</strong>
          <span
            @click="options2Fn(index, item.type)"
            :class="{ active: item.isActive }"
            v-for="(item, index) in options2"
            :key="index"
            >{{ item.text }}</span
          >
        </li>
      </ul>
      <List :arr="showGoodsList" />
      <p style="text-align: center">{{isBottom?"已经到底了":"页面加载中........"}}</p>
    </div>
  </div>
</template>
<script>
import Crumb from "../components/Crumb.vue";
import List from "../components/home/List.vue";
import { GetGoodsListAPI } from "@/request/api";
import { mapState } from "vuex";
import { getScrollHeight, getClientHeight, getScrollTop } from "@/utils";
export default {
  data() {
    return {
      //栏目 目前可提供的参数是 1->精品推荐 2-> 热门兑换 0->全部
      did: 0,
      //( 1->实体商品 2->虚拟商品 0->全部 )
      type: 0,
      //大于多少积分，最少是0
      min: 0,
      //少于多少积分，最多是10000，当传入0的时候，会直接返回所有商品，无视后台逻辑。
      max: 0,
      // 商品关键词
      keyword: "",
      //商品列表
      goodsList: [],
      //让页面只显示8条数据
      showGoodsList: [],

      //排序的数组项
      options1: [
        { min: 0, max: 10000, text: "全部" },
        { min: 0, max: 0, text: "我还可以兑换" },
        { min: 0, max: 500, text: "0-500" },
        { min: 500, max: 1000, text: "500 -1000" },
        { min: 1000, max: 1500, text: "1000-1500" },
        { min: 1500, max: 2500, text: "1500-2500" },
        { min: 2000, max: 6500, text: "2000-6500" },
        { min: 6500, max: 10000, text: "6500-10000" },
      ],
      //排序当前点击选项索引
      options1ActiveIndex: 0,
      options2: [
        {
          text: "全部",
          type: 0,
          isActive: true,
        },
        {
          text: "实物礼品",
          type: 1,
          isActive: false,
        },
        {
          text: "虚拟礼品",
          type: 2,
          isActive: false,
        },
      ],
      //每页容量
      size: 8,
      //当前页
      page: 1,
      //是否正在加载
      isLoading: false,
      //是否到底
      isBottom: false,
    };
  },
  computed: {
    ...mapState({ userInfo: (state) => state.userInfo.userInfo }),
  },
  methods: {
    //封装获取商品列表函数
    async searchGoods() {
      let res = await GetGoodsListAPI({
        did: this.did,
        type: this.ype,
        min: this.min,
        max: this.max,
        keyword: this.keyword,
      });
      if (!res) return;
      this.goodsList = res.data;
      this.showGoodsList = this.goodsList.filter((e, i) => i < 8);
      // console.log(this.showGoodsList);
      //页面没有刷新 可能导致导致page++
      //解决 初始化
      this.page = 1
      this.isBottom = false //默认数据加载中
      //如果没有查询到数据  goodlist = 0
      if(this.showGoodsList.length>=this.goodsList.length){
        this.isBottom = true
      }
    },
    //分类点击函数
    options2Fn(index, type) {
      this.options2.forEach((e, i) => {
        e.isActive = false;
      });
      this.options2[index].isActive = true;
      //修改点击选项
      this.type = type;
      this.searchGoods();
    },
    // 排序点击函数
    options1Fn(index, min, max) {
      this.options1ActiveIndex = index;
      //修改点击的选项
      this.min = min;
      this.max = max;
      this.searchGoods();
    },

    scrollFn() {
      //滑动滚轮触发的函数
      //滚出顶部距离
      // getScrollTop()
      //可视区域距离
      //  getClientHeight()
      //完整文档高度
      //  getScrollHeight()
      //如果数据已经加载完成改变底部提示
      if (this.goodsList.length <= this.showGoodsList.length) {
        this.isBottom = true;
        return;
      }


      //可视区域加上滚动距离>=文档高度-50的时候加载新数据
      if (!(getScrollTop() + getClientHeight() >= getScrollHeight() - 50))
        return;
      //问题：滑动过快的时候，会频繁进入循环
      //解决 让其延迟执行
      if (this.isLoading) return;
      //节流，判断是不是在加载中，如果在加载中则不进入循环
      //改变状态为加载状态
      this.isLoading = true;
      this.page++;
      setTimeout(() => {
        for (
          var i = this.size * (this.page - 1);
          i < this.size * this.page;
          i++
        ) {
          //短路运算
          this.goodsList[i] && this.showGoodsList.push(this.goodsList[i]);
        }
        //改状态为不在加载中
        this.isLoading = false;
      }, 500);
    },
  },
  mounted() {
    //添加滑动事件监听
    window.addEventListener("scroll", this.scrollFn);
  },
  created() {
    //关键字接从路由获取
    this.keyword = this.$route.query.keyword || "";
    this.searchGoods();
  },
  beforeDestroy() {
    //在销毁前接触事件监听
    window.removeEventListener("scroll", this.scrollFn);
  },
  watch: {
    //解决在商品页面进行搜索，路由该改变，但是没有调用create不能搜索的问题

    "$route.query.keyword": {
      handler(newVal, oldVal) {
        if (newVal != oldVal) {
          this.keyword = newVal;
          this.searchGoods();
        }
      },
    },
  },
  components: {
    Crumb,
    List,
  },
};
</script>

<style lang="scss" scoped>
@import "../assets/css/global.css";

.goods {
  padding-bottom: 50px;
  background: #efefef;
  padding-top: 20px;
  .options {
    padding-top: 20px;
    padding-bottom: 40px;
    li {
      margin-top: 20px;
      strong {
        color: #000;
        font-weight: bold;
      }
      span {
        margin-right: 20px;
        margin-left: 10px;
        cursor: pointer;
        color: #999;
        &.active {
          color: #415fe6;
          font-weight: bold;
        }
      }
    }
  }
}
</style>
